<template>
  <l-panel style="padding: 0">
    <template #toolLeft>
      <l-query2
        :labelWidth="0"
        :popoverWidth="240"
        :items="queryItems"
        :formData="queryData"
        :loading="tableLoading"
        @search="handleSearch"
      >
        <template #keyWord>
          <el-input
            v-model="queryData.keyWord"
            :placeholder="$t('请输入查询关键字')"
          ></el-input>
        </template>
        <template #seachDate>
          <l-date
            format="yyyy-MM-dd"
            dateType="daterange"
            v-model="queryData.seachDate"
            :startPlaceholder="$t('开始时间')"
            :endPlaceholder="$t('结束时间')"
          >
          </l-date>
        </template>
      </l-query2>
    </template>
    <template #toolRight>
      <l-tool-btns :hasAdd="false">
        <el-button-group>
          <el-button type="primary" size="mini" @click="openBatchAudit()">{{
            $t("批量审核")
          }}</el-button>
        </el-button-group>
      </l-tool-btns>
    </template>
    <l-table
      ref="mainTable"
      :loading="tableLoading"
      :columns="columns"
      :dataSource="tableData"
      :isPage="true"
      :pageTotal="tableTotal"
      :tablePage.sync="tableCurrentPage"
      @loadPageData="turnTablePage"
    >
      <template v-slot:f_UnitName="scope">
        <el-button @click="handleAudit(scope.row)" size="mini" type="text">{{
          scope.row.f_UnitName
        }}</el-button>
      </template>

      <template v-slot:f_IsUrge="scope">
        <el-tag v-if="scope.row.f_IsUrge == 1" size="mini" type="warning"
          >{{$t('催办')}}</el-tag
        >
        <el-tag v-else size="mini" type="success">{{ $t('正常') }}</el-tag>
      </template>
      <template v-slot:f_CreateDate="scope">
        {{ lr_dateFormat(scope.row.f_CreateDate) }}
      </template>
    </l-table>

    <wf-audit-form
      ref="form"
      type="audit"
      :isCreateAgain="isCreateAgain"
      :isCreateSub="isCreateSub"
      :params="{ taskId: taskId }"
      :isLoadFormData="true"
      :title="title"
      :visible.sync="visible"
      @refresh="loadTableData(true)"
    ></wf-audit-form>
    <l-dialog
      :title="$t('批量审核')"
      :visible.sync="batchVisible"
      :width="1000"
      :height="600"
      @close="handleBatchCloseForm"
      @opened="handleBatchOpenedForm"
      ref="batchDialog"
    >
      <audit-batch-index ref="batchAudit" />
      <template #btns>
        <el-button size="mini" @click="handleBatchClose">{{
          $t("取消")
        }}</el-button>
        <el-button size="mini" type="danger" @click="handleBatchAudit(false)">{{
          $t("不同意")
        }}</el-button>
        <el-button size="mini" type="primary" @click="handleBatchAudit(true)">{{
          $t("同意")
        }}</el-button>
      </template>
    </l-dialog>
  </l-panel>
</template>

<script>
const api = window.$api.workflow.process;

import AuditBatchIndex from "./auditBatchIndex.vue";
export default {
  components: {
    AuditBatchIndex,
  },
  data() {
    return {
      queryItems: [
        { prop: "seachDate", span: 24 },
        { prop: "keyWord", span: 24 },
      ],
      queryData: {
        keyWord: "",
        seachDate: "",
      },
      // 表格参数
      tableLoading: false,
      tableData: [],
      tableTotal: 0,
      tableCurrentPage: 1,
      tablePageSize: 50,
      columns: [
        { label: "任务", prop: "f_UnitName", minWidth: 100, align: "left" },
        { label: "标题", prop: "f_ProcessTitle", minWidth: 100, align: "left" },
        { label: "状态", prop: "f_IsUrge", width: 80, align: "center" },
        {
          label: "提交人",
          prop: "f_ProcessUserId",
          width: 250,
          align: "left",
          dataType:'fullUser',
          isNotAutoWrap:true
        },
        { label: "时间", prop: "f_CreateDate", width: 160, align: "left" },
      ],

      taskId: "",
      title: "",
      visible: false,
      isCreateAgain: false,
      isCreateSub: false,

      batchVisible: false,
    };
  },
  computed: {},
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.loadTableData();
    },
    handleSearch() {
      this.loadTableData();
    },
    turnTablePage({ rows }) {
      this.tablePageSize = rows;
      this.loadTableData(true);
    },
    async loadTableData(isNotFirst) {
      if (!isNotFirst) {
        this.tableCurrentPage = 1;
      }
      this.tableLoading = true;
      const queryData = this.$deepClone(this.queryData);

      if (queryData.seachDate) {
        const dateList = queryData.seachDate.split(" - ");
        queryData.startDate = dateList[0];
        queryData.endDate = dateList[1];
        queryData.seachDate = null;
      }

      const pagination = {
        rows: this.tablePageSize,
        page: this.tableCurrentPage,
        sidx: "f_IsUrge DESC,t.F_CreateDate DESC"
      };

      /*queryData.rows = this.tablePageSize;
      queryData.page = this.tableCurrentPage;
      queryData.sidx = "f_IsUrge DESC,F_CreateDate DESC";*/
      let data = await this.$awaitWraper(api.getUnCompletedMyPage(pagination,queryData));
      if (data != null) {
        this.tableData = data.rows;
        this.tableTotal = data.records;
      } else {
        this.tableData = [];
      }
      this.tableLoading = false;
    },
    handleAudit(row) {
      this.isCreateAgain = row.f_Type == 4 ? true : false;
      this.isCreateSub = row.f_Type == 3 ? true : false;
      this.taskId = row.f_Id;
      this.title = `${row.f_ProcessTitle}【${row.f_UnitName}】`;
      this.visible = true;

      /*let routeUrl = this.$router.resolve({
        path: `/workflow/info?id=${row.f_ProcessId}`,
      });
      window.open(routeUrl.href, "_blank");*/
    },

    // 批量审核
    openBatchAudit() {
      this.batchVisible = true;
    },
    handleBatchOpenedForm() {
      this.$refs.batchAudit.reset();
    },
    handleBatchCloseForm() {},
    handleBatchClose() {
      this.batchVisible = false;
    },
    handleBatchAudit(flag) {
      const value = this.$refs.batchAudit.value;
      if (!value) {
        return;
      }
      this.$confirm(
        this.$t(`是否${flag ? "同意" : "不同意"}所选流程?`),
        this.$t("提示"),
        {
          confirmButtonText: this.$t("确定"),
          cancelButtonText: this.$t("取消"),
          type: "warning",
        }
      )
        .then(async () => {
          this.$refs.batchDialog.showLoading('审核中...')
          await api.audits(flag?'agree':'disagree',{ids:value})
          this.$refs.batchDialog.hideLoading()
          this.$refs.batchAudit.reset();

        })
        .catch(() => {});
    },
  },
};
</script>